package com.sleep.psy.external_server.business.evaluation.service.impl;

import com.alibaba.fastjson.JSON;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubGeneralScaleReq;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubQuestionReq;
import com.sleep.psy.external_server.business.evaluation.entity.sleep.SubSleepScaleReq;
import com.sleep.psy.external_server.business.evaluation.service.DealQuestionService;
import com.sleep.psy.external_server.business.psy.entity.PsyAnswer;
import com.sleep.psy.external_server.business.psy.entity.PsyScore;
import com.sleep.psy.external_server.business.psy.entity.PsySection;
import com.sleep.psy.external_server.business.psy.mapper.PsyAnswerMapper;
import com.sleep.psy.external_server.business.psy.mapper.PsyScoreMapper;
import com.sleep.psy.external_server.business.psy.mapper.PsySectionMapper;
import com.sleep.psy.external_server.constants.CommonEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author wenliguo
 * @date 2025年06月19日 11:08
 */
@Service
public class DealQuestionServiceImpl implements DealQuestionService {

    protected Logger logger = LoggerFactory.getLogger(DealQuestionService.class);

    @Autowired
    PsySectionMapper psySectionMapper;

    @Autowired
    PsyAnswerMapper psyAnswerMapper;

    @Autowired
    PsyScoreMapper psyScoreMapper;

    /**
     * 处理匹兹堡
     * @author wenliguo
     * @date 2025/6/5 20:08
     * @param req 请求
     * @param reportId 报告id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subSleepAnswer(SubSleepScaleReq req, Long reportId) {

        logger.info("处理匹兹堡");
        Map<String, Object> paramsMap = JSON.parseObject(req.getAnswer());
        List<Map<String, Object>> answerMapList = (List<Map<String, Object>>) paramsMap.get("answer");
        // PsyAnswer 信息
        PsyAnswer psyAnswer = null;

        // PsyScore 信息
        PsyScore psyScore = null;

        /**
         * 因子-睡眠质量 睡眠质量 为ID=15选择的分数 originScore=对应的分数
         */

        List<Integer> psqi_SUBJECTIVE_SLEEP_QUALITY_Ids = Arrays.asList(15);
        double psqi_SUBJECTIVE_SLEEP_QUALITY = 0;

        /**
         * 因子-入睡时间 入睡时间 为ID=2和3的分数和
         */
        List<Integer> psqi_SLEEP_LATENCY_Ids = Arrays.asList(2, 5);
        double psqi_SLEEP_LATENCY = 0;

        /**
         * 因子-睡眠时间 睡眠时间 = 变量b 睡眠时间为id=4选择的分数 当value>7 originScore=0 当6<=value<=7
         * originScore=1 当5<=value<6 originScore=2 否则 originScore=3
         */
        List<Integer> psqi_SLEEP_DURATION_Ids = Arrays.asList(4);
        double psqi_SLEEP_DURATION = 0;

        /**
         * 因子-睡眠效率 睡眠效率 = 变量b/变量a 变量a 床上时间为id=1选择的分数 变量b 睡眠时间为id=4选择的分数
         */
        List<Integer> psqi_SLEEP_EFFICIENCY_Ids = Arrays.asList(1);
        double psqi_SLEEP_EFFICIENCY = 0;

        /**
         * 因子-睡眠障碍 睡眠障碍 为id= 6, 7, 8, 9, 10, 11, 12, 13, 14 的分数的和 当value=0 originScore=0
         * 当0<value<=9 originScore=1 当9<value<=18 originScore=2 否则 originScore=3
         */
        List<Integer> psqi_SLEEP_DISTURBANCE_Ids = Arrays.asList(6, 7, 8, 9, 10, 11, 12, 13, 14);
        double psqi_SLEEP_DISTURBANCE = 0;

        /**
         * 因子-催眠药物 催眠药物 为id= 16,的分数 当value=0 originScore=0 当0<value<=9 originScore=1
         * 当9<value<=18 originScore=2 否则 originScore=3
         */
        List<Integer> psqi_USE_OF_SLEEP_MEDICATION_Ids = Arrays.asList(16);
        double psqi_USE_OF_SLEEP_MEDICATION = 0;

        /**
         * 因子-日间功能障碍 日间功能障碍 为id= 17,18 的分数的和 当value=0 originScore=0 当0<value<=2
         * originScore=1 当2<value<=4 originScore=2 否则 originScore=3
         */
        List<Integer> psqi_DAYTIME_DYSFUNCTION_Ids = Arrays.asList(17, 18);
        double psqi_DAYTIME_DYSFUNCTION = 0;

        /**
         * 总分 总分 =睡眠质量+入睡时间+睡眠时间+睡眠效率+睡眠障碍+催眠药物+日间功能障碍
         */
        double psqi_TOTAL = 0;

        Integer answerSortIndex = 0;
        List<Map<String, Object>> sectionList = new ArrayList<>();

        PsySection psySection = new PsySection();

        for (Map<String, Object> map : answerMapList) {

            sectionList = (List<Map<String, Object>>) map.get("sections");
            for (Map<String, Object> selMap : sectionList) {
                answerSortIndex += 1;

                psyAnswer = new PsyAnswer();
                psyAnswer.setSortIndex(answerSortIndex);
                psyAnswer.setReportId(reportId);
                psyAnswer.setStem(map.get("stem") + "");
                psyAnswerMapper.insert(psyAnswer);

                psySection = new PsySection();
                psySection.setReportId(reportId);
                psySection.setSelectedIndex(Integer.valueOf(selMap.get("selectedIndex") + ""));
                psySection.setTemplate(selMap.get("template") + "");
                psySection.setSortIndex(Integer.valueOf(selMap.get("sortIndex") + ""));
                psySection.setValue(Double.valueOf(selMap.get("value") + ""));
                psySection.setAnswerId(psyAnswer.getAnswerId());
                psySection.setText(selMap.get("text") + "");
                psySectionMapper.insert(psySection);

                if (psqi_SUBJECTIVE_SLEEP_QUALITY_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_SUBJECTIVE_SLEEP_QUALITY += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }

                if (psqi_SLEEP_LATENCY_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_SLEEP_LATENCY += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }

                if (psqi_SLEEP_DURATION_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_SLEEP_DURATION += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }

                if (psqi_SLEEP_DISTURBANCE_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_SLEEP_DISTURBANCE += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }

                if (psqi_USE_OF_SLEEP_MEDICATION_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_USE_OF_SLEEP_MEDICATION += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }

                if (psqi_DAYTIME_DYSFUNCTION_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_DAYTIME_DYSFUNCTION += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }

                if (psqi_SLEEP_EFFICIENCY_Ids.contains(Integer.valueOf(selMap.get("sortIndex") + ""))) {
                    psqi_SLEEP_EFFICIENCY += Integer.parseInt(selMap.get("value") + "");
                    continue;
                }
            }
        }

        psyScore = new PsyScore();
        psyScore.setSortIndex(1);
        psyScore.setClassName("psqi.SUBJECTIVE_SLEEP_QUALITY");
        psyScore.setDefaultName("睡眠质量");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(psqi_SUBJECTIVE_SLEEP_QUALITY);
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += psqi_SUBJECTIVE_SLEEP_QUALITY;

        Integer originScore = 0;
        if (psqi_SLEEP_LATENCY == 0) {
            originScore = 0;
        } else if (psqi_SLEEP_LATENCY > 0 && psqi_SLEEP_LATENCY < 3) {
            originScore = 1;
        } else if (psqi_SLEEP_LATENCY >= 3 && psqi_SLEEP_LATENCY < 4) {
            originScore = 2;
        } else {
            originScore = 3;
        }
        psyScore = new PsyScore();
        psyScore.setSortIndex(2);
        psyScore.setClassName("psqi.SLEEP_LATENCY");
        psyScore.setDefaultName("入睡时间");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(Double.valueOf(originScore));
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += originScore;

        if (psqi_SLEEP_DURATION > 7) {
            originScore = 0;
        } else if (psqi_SLEEP_DURATION >= 6) {
            originScore = 1;
        } else if (psqi_SLEEP_DURATION >= 5) {
            originScore = 2;
        } else {
            originScore = 3;
        }

        psyScore = new PsyScore();
        psyScore.setSortIndex(3);
        psyScore.setClassName("psqi.SLEEP_DURATION");
        psyScore.setDefaultName("睡眠时间");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(Double.valueOf(originScore));
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += originScore;

        psqi_SLEEP_EFFICIENCY = psqi_SLEEP_DURATION / psqi_SLEEP_EFFICIENCY;
        if (psqi_SLEEP_EFFICIENCY > 0.85) {
            originScore = 0;
        } else if (psqi_SLEEP_EFFICIENCY >= 0.75) {
            originScore = 1;
        } else if (psqi_SLEEP_EFFICIENCY >= 0.65) {
            originScore = 2;
        } else {
            originScore = 3;
        }

        psyScore = new PsyScore();
        psyScore.setSortIndex(4);
        psyScore.setClassName("psqi.SLEEP_EFFICIENCY");
        psyScore.setDefaultName("睡眠效率");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(Double.valueOf(originScore));
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += originScore;

        if (psqi_SLEEP_DISTURBANCE == 0) {
            originScore = 0;
        } else if (psqi_SLEEP_DISTURBANCE > 0 && psqi_SLEEP_DISTURBANCE <= 9) {
            originScore = 1;
        } else if (psqi_SLEEP_DISTURBANCE > 9 && psqi_SLEEP_DISTURBANCE <= 18) {
            originScore = 2;
        } else {
            originScore = 3;
        }
        psyScore = new PsyScore();
        psyScore.setSortIndex(5);
        psyScore.setClassName("psqi.SLEEP_DISTURBANCE");
        psyScore.setDefaultName("睡眠障碍");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(Double.valueOf(originScore));
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += originScore;

        if (psqi_USE_OF_SLEEP_MEDICATION == 0) {
            originScore = 0;
        } else if (psqi_USE_OF_SLEEP_MEDICATION > 0 && psqi_USE_OF_SLEEP_MEDICATION <= 9) {
            originScore = 1;
        } else if (psqi_USE_OF_SLEEP_MEDICATION > 9 && psqi_USE_OF_SLEEP_MEDICATION <= 18) {
            originScore = 2;
        } else {
            originScore = 3;
        }

        psyScore = new PsyScore();
        psyScore.setSortIndex(6);
        psyScore.setClassName("psqi.USE_OF_SLEEP_MEDICATION");
        psyScore.setDefaultName("催眠药物");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(Double.valueOf(originScore));
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += originScore;

        if (psqi_DAYTIME_DYSFUNCTION == 0) {
            originScore = 0;
        } else if (psqi_DAYTIME_DYSFUNCTION > 0 && psqi_DAYTIME_DYSFUNCTION <= 2) {
            originScore = 1;
        } else if (psqi_DAYTIME_DYSFUNCTION > 2 && psqi_DAYTIME_DYSFUNCTION <= 4) {
            originScore = 2;
        } else {
            originScore = 3;
        }
        psyScore = new PsyScore();
        psyScore.setSortIndex(7);
        psyScore.setClassName("psqi.DAYTIME_DYSFUNCTION");
        psyScore.setDefaultName("日间功能障碍");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(Double.valueOf(originScore));
        psyScore.setScoreType(2);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
        psqi_TOTAL += originScore;

        psyScore = new PsyScore();
        psyScore.setSortIndex(0);
        psyScore.setClassName("psqi.TOTAL");
        psyScore.setDefaultName("总分");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(psqi_TOTAL);
        psyScore.setScoreType(0);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);

    }


    /**
     * 处理通用量表
     * @author wenliguo
     * @date 2025/7/23 10:16
     * @param req 请求
     * @param reportId 报告id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subGeneralScale(SubGeneralScaleReq req, Long reportId) {
        logger.info("处理通用量表");
        Map<String, Object> paramsMap = JSON.parseObject(req.getAnswer());
        List<Map<String, Object>> answerMapList = (List<Map<String, Object>>) paramsMap.get("answer");
        // PsyAnswer 信息
        PsyAnswer psyAnswer = null;

        // PsyScore 信息
        PsyScore psyScore = null;

        int answerSortIndex = 0;
        List<Map<String, Object>> sectionList;

        //提交分数详细表
        PsySection psySection;
        for (Map<String, Object> map : answerMapList) {
            sectionList = (List<Map<String, Object>>) map.get("sections");
            for (Map<String, Object> selMap : sectionList) {
                answerSortIndex += 1;

                psyAnswer = new PsyAnswer();
                psyAnswer.setSortIndex(answerSortIndex);
                psyAnswer.setReportId(reportId);
                psyAnswer.setStem(map.get("stem") + "");
                psyAnswerMapper.insert(psyAnswer);

                psySection = new PsySection();
                psySection.setSelectedIndex(Integer.valueOf(selMap.get("selectedIndex") + ""));
                psySection.setSelectedOptions(selMap.get("options") + "");
                psySection.setTemplate(selMap.get("template") + "");
                psySection.setSortIndex(Integer.valueOf(selMap.get("sortIndex") + ""));
                psySection.setValue(Double.valueOf(selMap.get("value") + ""));
                psySection.setAnswerId(psyAnswer.getAnswerId());
                psySection.setText(selMap.get("text") + "");
                psySection.setReportId(reportId);
                psySectionMapper.insert(psySection);

            }
        }


    }

    /**
     * 处理ISI量表
     * @author wenliguo
     * @date 2025/7/9 16:33
     * @param req 请求
     * @param reportId 报告id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subIsi(SubSleepScaleReq req, Long reportId) {
        logger.info("处理ISI量表");
        Map<String, Object> paramsMap = JSON.parseObject(req.getAnswer());
        List<Map<String, Object>> answerMapList = (List<Map<String, Object>>) paramsMap.get("answer");
        // PsyAnswer 信息
        PsyAnswer psyAnswer = null;

        // PsyScore 信息
        PsyScore psyScore = null;

        int answerSortIndex = 0;
        List<Map<String, Object>> sectionList = new ArrayList<>();

        PsySection psySection = new PsySection();
        Double totalScore = 0D;
        for (Map<String, Object> map : answerMapList) {

            sectionList = (List<Map<String, Object>>) map.get("sections");
            for (Map<String, Object> selMap : sectionList) {
                answerSortIndex += 1;

                psyAnswer = new PsyAnswer();
                psyAnswer.setSortIndex(answerSortIndex);
                psyAnswer.setReportId(reportId);
                psyAnswer.setStem(map.get("stem") + "");
                psyAnswerMapper.insert(psyAnswer);

                psySection = new PsySection();
                psySection.setReportId(reportId);
                psySection.setSelectedIndex(Integer.valueOf(selMap.get("selectedIndex") + ""));
                psySection.setTemplate(selMap.get("template") + "");
                psySection.setSortIndex(Integer.valueOf(selMap.get("sortIndex") + ""));
                psySection.setValue(Double.valueOf(selMap.get("value") + ""));
                psySection.setAnswerId(psyAnswer.getAnswerId());
                psySection.setText(selMap.get("text") + "");
                psySectionMapper.insert(psySection);

                //分数
                if (1 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(1);
                    psyScore.setClassName("isi.RU_SHUI_KUN_NAN_CHENG_DU");
                    psyScore.setDefaultName("入睡困难程度");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                } else if (2 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(2);
                    psyScore.setClassName("isi.WEI_CHI_SHUI_MIAN_KUN_NAN_CHENG_DU");
                    psyScore.setDefaultName("维持睡眠困难程度");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                } else if (3 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(3);
                    psyScore.setClassName("isi.ZAO_SHUI_PIN_LV");
                    psyScore.setDefaultName("早醒频率");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                } else if (4 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(4);
                    psyScore.setClassName("isi.DUI_DANG_QIAN_SHUI_MIAN_MAN_YI_DU");
                    psyScore.setDefaultName("对当前睡眠满意度");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                } else if (5 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(5);
                    psyScore.setClassName("isi.SHUI_MIAN_WEN_TI_GAN_RAO_RI_JIAN_GONG_NENG");
                    psyScore.setDefaultName("睡眠问题干扰日间功能");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                } else if (6 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(6);
                    psyScore.setClassName("isi.TA_REN_SHI_FOU_ZHU_YI_DAO_NI_DE_ZHUANGT_TAI");
                    psyScore.setDefaultName("他人是否注意到你的状态");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                } else if (7 == Integer.parseInt(selMap.get("sortIndex") + "")) {
                    psyScore = new PsyScore();
                    psyScore.setSortIndex(7);
                    psyScore.setClassName("isi.DUI_SHI_MIAN_DE_DAN_YOU_CHENG_DU");
                    psyScore.setDefaultName("对失眠的担忧程度");
                    psyScore.setStandardScore(null);
                    psyScore.setOriginScore(Double.valueOf(selMap.get("value") + ""));
                    psyScore.setScoreType(2);
                    psyScore.setAbnormalLevel(0);
                    psyScore.setReportId(reportId);
                    psyScoreMapper.insert(psyScore);
                    totalScore += Double.parseDouble(selMap.get("value") + "");
                }

            }
        }
        psyScore = new PsyScore();
        psyScore.setSortIndex(0);
        psyScore.setClassName("isi.TOTAL");
        psyScore.setDefaultName("总分");
        psyScore.setStandardScore(null);
        psyScore.setOriginScore(totalScore);
        psyScore.setScoreType(0);
        psyScore.setAbnormalLevel(0);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
    }

    /**
     * 处理睡眠问卷
     * @author wenliguo
     * @date 2025/6/19 12:42
     * @param req 请求
     * @param reportId 报告id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subSleepQuestionAnswer(SubQuestionReq req, Long reportId) {
        logger.info("处理睡眠问卷");
        Map<String, Object> paramsMap = JSON.parseObject(req.getAnswer());
        List<Map<String, Object>> answerMapList = (List<Map<String, Object>>) paramsMap.get("answer");
        // PsyAnswer 信息
        PsyAnswer psyAnswer = null;

        // PsyScore 信息
        PsyScore psyScore = null;

        Integer answerSortIndex = 0;
        List<Map<String, Object>> sectionList;

        //提交分数详细表
        PsySection psySection = new PsySection();
        for (Map<String, Object> map : answerMapList) {
            sectionList = (List<Map<String, Object>>) map.get("sections");
            for (Map<String, Object> selMap : sectionList) {
                answerSortIndex += 1;

                psyAnswer = new PsyAnswer();
                psyAnswer.setSortIndex(answerSortIndex);
                psyAnswer.setReportId(reportId);
                psyAnswer.setStem(map.get("stem") + "");
                psyAnswerMapper.insert(psyAnswer);

                psySection = new PsySection();
                psySection.setSelectedIndex(Integer.valueOf(selMap.get("selectedIndex") + ""));
                psySection.setTemplate(selMap.get("template") + "");
                psySection.setSortIndex(Integer.valueOf(selMap.get("sortIndex") + ""));
                psySection.setValue(Double.valueOf(selMap.get("value") + ""));
                psySection.setAnswerId(psyAnswer.getAnswerId());
                psySection.setText(selMap.get("text") + "");
                psySection.setReportId(reportId);
                psySectionMapper.insert(psySection);

            }
        }

        psyScore = new PsyScore();
        psyScore.setSortIndex(0);
        psyScore.setClassName(CommonEnum.InventoryEnum.AUDIO_CURE.getCode());
        psyScore.setDefaultName("治疗等级");
        psyScore.setStandardScore(0d);
        psyScore.setOriginScore(0d);
        psyScore.setScoreType(0);
        psyScore.setAbnormalLevel(3);
        psyScore.setReportId(reportId);
        psyScoreMapper.insert(psyScore);
    }

    /**
     * 处理失眠问卷
     * @author wenliguo
     * @date 2025/9/1 09:22
     * @param req 请求
     * @param reportId 报告id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void subSleepLessScale(SubGeneralScaleReq req, Long reportId) {
        logger.info("处理失眠问卷");
        Map<String, Object> paramsMap = JSON.parseObject(req.getAnswer());
        List<Map<String, Object>> answerMapList = (List<Map<String, Object>>) paramsMap.get("answer");
        // PsyAnswer 信息
        PsyAnswer psyAnswer = null;

        // PsyScore 信息
        PsyScore psyScore = null;

        Integer answerSortIndex = 0;
        List<Map<String, Object>> sectionList;

        //提交分数详细表
        PsySection psySection = new PsySection();

        for (Map<String, Object> map : answerMapList) {
                answerSortIndex += 1;

                psyAnswer = new PsyAnswer();
                psyAnswer.setSortIndex(answerSortIndex);
                psyAnswer.setReportId(reportId);
                psyAnswer.setStem("_blank");
                psyAnswerMapper.insert(psyAnswer);

                psySection = new PsySection();
                psySection.setTemplate(map.get("template") + "");
                psySection.setAnswerId(psyAnswer.getAnswerId());
                psySection.setText(map.get("text") + "");
                psySection.setReportId(reportId);
                psySectionMapper.insert(psySection);

        }
    }
}
